/**
 * Jan 24, 2021  copy right
 */
package com.code260.redstar.testkit;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author code260@qq.com
 *
 */
public class TestMainKit {
	
	private static  volatile boolean inited;
	
	public static boolean init() {
		if (!inited) {
			
			/**
			 * 初始化逻辑，暂无
			 */
			inited = true;
		}
		return true;
	}
	
	/**
	 * 挂起指定时间，用于注入暂停场景
	 * @param sleeptimeMS
	 * @return
	 * @throws InterruptedException
	 */
	public static boolean sleep(long sleeptimeMS) throws InterruptedException {
		TimeUnit.MILLISECONDS.sleep(sleeptimeMS);
		return true;
	}
	
	public static boolean sysout(Object obj) {
		return sysout("", obj);
	}
	
	/**
	 * 将相应的信息和时间戳写到标准输出
	 * @param name
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean sysout(String name, Object obj) {
		String tempName = name;
		if (tempName == null) {
			tempName = "";
		}
		StringBuffer messagesb = new StringBuffer();
		if (obj == null) {
			messagesb.append("null");
		}
		if (obj instanceof Iterable) {
			messagesb.append("[");
			for (Object child : (Iterable)obj){
				if (child == null) {
					messagesb.append("null");
				} else {
					messagesb.append(child.toString());
				}
				messagesb.append(",");
			}
			messagesb.append("]");
		} else {
			messagesb.append(obj.toString());
		}
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MMM-ddd hhh:mmm:sss");
		System.out.println(sdf.format(new Date()) + "" + Thread.currentThread().getName() 
				+ " " + tempName + " " + messagesb.toString());
		return true;
	}

}
